package com.iRomantic.controller.manager;

import java.io.File;
import java.util.UUID;

import com.iRomantic.common.BaseController;
import com.iRomantic.common.oss.OSSService;
import com.iRomantic.common.util.FileUtil;
import com.iRomantic.config.Constant;
import com.iRomantic.domain.entity.Hotel;
import com.iRomantic.domain.entity.HotelPicture;
import com.jfinal.upload.UploadFile;

public class HotelController extends BaseController {
	
	/**
	 * 创建婚礼现场
	 * @throws Exception 
	 */
	public void create() throws Exception{
		String path = this.getSession().getServletContext().getRealPath(Constant.TEMP_FOLDER);
		
		//图片处理
		UploadFile picture = this.getFile("hotel.picture",path);
		Hotel hotel = this.getModel(Hotel.class);
		hotel.set("id", UUID.randomUUID().toString());
		if(picture!=null){
			String pictureName = FileUtil.randomFileName(picture.getFileName());
			OSSService.getInstance().save(Constant.HOTEL_PICTURE+hotel.getStr("hotelId")+"/"+pictureName, picture.getFile());
			picture.getFile().delete();
			hotel.set("pictureUrl", pictureName);
		}
		hotel.save();
		
		this.renderJson(hotel);
	}
	
	/**
	 * 上传婚礼现场照片
	 * @throws Exception 
	 */
	public void createPicture() throws Exception{
		String path = this.getSession().getServletContext().getRealPath(Constant.TEMP_FOLDER);
		
		//图片处理
		UploadFile picture = this.getFile("file",path);
		HotelPicture currPicture = this.getModel(HotelPicture.class);
		String pictureName = FileUtil.randomFileName(picture.getFileName());
		OSSService.getInstance().save(Constant.HOTEL_PICTURE+currPicture.getStr("hotelId")+"/"+pictureName, picture.getFile());
		picture.getFile().delete();
		
		currPicture.set("url", pictureName);
		currPicture.save();
		
		this.renderJson(currPicture);
		
	}

}
